import { setEncrypt_token,setCXBalance } from '@/utils/auth'
import {resetSetItem} from "@/utils/pub-use"
window.Chengxin = function (info: any) {
	window.Chengxin.prototype._init_(info)
	/*  this._init_(info); */
}
/* function Chengxin(info) {
   Chengxin.prototype._init_(info);
} */
window.Chengxin.prototype = {
	constructor: window.Chengxin,
	_init_: function (info: any) {
		this.encryptToken = info.encryptToken
		this.nocallback = info.nocallback
		this.paycallback = info.paycallback
		this.share = info
	},
	
	
	
	//调用app获取当前登录用户加密token
	getEncryptToken: function () {
		
		//调用app的方法
		/* this.notifyEncryptToken(this.nocallback.toString(),
           '5wj9UpJ4KdXW/QUG/4uW2vgbiPXqKmKP7RoZseydX+2NQ1qS3MccbCWDNxscyyGL') */
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			if (typeof window.webkit != 'undefined') {
				//是否为ioswebview
				if (typeof window.webkit.messageHandlers != 'undefined') {
					if (
						typeof window.webkit.messageHandlers.getEncryptToken != 'undefined'
					) {
						window.webkit.messageHandlers.getEncryptToken.postMessage(
							this.nocallback.toString()
						) //调用ios方法
					}
				}
			}
		} else if (/(Android)/i.test(navigator.userAgent)) {
			if (typeof window.myjs != 'undefined') {
				window.myjs.getEncryptToken(this.nocallback.toString()) //myjs 是android端和js协商定义的调用对象, getEncryptToken 是方法名,
			}
			/* this.notifyEncryptToken(this.nocallback.toString(),
            '5wj9UpJ4KdXW/QUG/4uW2vgbiPXqKmKP7RoZseydX+2NQ1qS3MccbCWDNxscyyGL','uOdccE8Fw6an6YFIg1pdxrTWLsVNmghQ9FoR5kpFvhXZ3bylO8ZvlxYMUVhLaCyu') */
		}
	},
	//app回调返回当前登录用户加密Ttoken
	notifyEncryptToken: async function (callback: any, encryptToken: any) {
		
		setEncrypt_token(encryptToken)
		return encryptToken
	},

	//调起app支付页面
	startPayment: function (
		payorder_no: any,
		total_fee: any,
		order_body: any,
		pay_type: any
	) {
		// alert("调起支付页面" + this.paycallback);
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			var param = {
				payorder_no: payorder_no,
				total_fee: total_fee,
				order_body: order_body,
				pay_type: pay_type,
				paycallback: 'chengxinPaycallback'
			}
			window.webkit.messageHandlers.startPayment.postMessage(param) //调用ios方法
		} else if (/(Android)/i.test(navigator.userAgent)) {
			window.myjs.startPayment(
				payorder_no,
				total_fee,
				order_body,
				pay_type,
				'chengxinPaycallback'
			) //myjs 是android端和js协商定义的调用对象, startPayment 是方法名,
		}
	},
	//app支付结果回调通知
	notifyPayResult: function (
		payorder_no: any,
		total_fee: any,
		order_body: any,
		payStatus: any,
		paycallback: any
	) {
		resetSetItem("pay_status",payStatus)
		// setPayStatus(payStatus)
		// return("页面返回:payorder_no=" + payorder_no + "++total_fee=" + total_fee + "++order_body=" + order_body + "++payResult=" + payStatus);
		var excallback = eval('(' + paycallback + ')')
		return excallback(payorder_no, total_fee, order_body, payStatus)
	},

	//调用app判断是否开通钱包
	getWalletStatus:function(){
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //调用ios方法
			window.webkit.messageHandlers.getWalletStatus.postMessage('') //调用ios方法
		} else if (/(Android)/i.test(navigator.userAgent)) {
			window.myjs.getWalletStatus()
		}
	},

	notifyWalletStatus:function(walletStatus:any){
		if(walletStatus == 1 || walletStatus == '1'){
			getCheckxinToken().getEncryptToken()
			getBalanceExport().getBalance()
		}
	},

	//分享的信息
	shareInfo: function () {
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			window.webkit.messageHandlers.transmitInfo.postMessage(this.share) //调用ios方法
		} else if (/(Android)/i.test(navigator.userAgent)) {
			return this.share //将分享信息直接返回
		}
	},
	addFriend: function (accid: any) {
		var accid: any = {
			accid: accid
		}
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			window.webkit.messageHandlers.addFriend.postMessage(accid) //调用ios方法
		} else if (/(Android)/i.test(navigator.userAgent)) {
			window.myjs.addFriend(JSON.stringify(accid)) //myjs 是android端和js协商定义的调用对象, addFriend 是方法名,
		}
	},
	//网页全屏
	fullscreen: function (isFull: any, suspension: any) {
		var mes = {
			fullscreen: isFull,
			suspension: suspension
		}
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			if (typeof window.webkit.messageHandlers.setFullscreen != 'undefined') {
				window.webkit.messageHandlers.setFullscreen.postMessage(mes) //调用ios方法
			}
		} else if (/(Android)/i.test(navigator.userAgent)) {
			if (window.myjs.setFullscreen) {
				window.myjs.setFullscreen(JSON.stringify(mes))
			}
		}
	},

	//查询钱包余额
	getBalance:function(){
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			window.webkit.messageHandlers.getBalance.postMessage('') //调用ios方法
		} else if (/(Android)/i.test(navigator.userAgent)) {
			window.myjs.getBalance()
		}
	},

	notifyBalance: function (balance: any) {
		setCXBalance(balance)
	},


	//提现功能
	toWithdraw:function(){
		if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
			window.webkit.messageHandlers.toWithdraw.postMessage('') //调用ios方法
		} else if (/(Android)/i.test(navigator.userAgent)) {
			window.myjs.toWithdraw()
		}
	},
}


function getBalanceExport(){
	return new window.Chengxin({callback:'notifyBalance'})
}

function getWalletStatusExport (){
	return new window.Chengxin({ callback: 'notifyWalletStatus' })
}

function getCheckxinToken() {
	return new window.Chengxin({ nocallback: 'getUserEncryptToken' })
}

function getStartPayment() {
	return new window.Chengxin({ paycallback: 'chengxinPaycallback' })
}

function getToWithdrawExport(){
	return new window.Chengxin({callback:'toWithdraw'})
}

function addNewFriend(accid: any) {
	var obj = new window.Chengxin({ paycallback: 'chengxinPaycallback' })
	obj.addFriend(accid)
}

function setfullscreen(isFull: any, suspension: any) {
	var obj = new window.Chengxin({ paycallback: 'chengxinPaycallback' })
	obj.fullscreen(isFull, suspension)
}


function chengxinPaycallback(
	payorder_no: any,
	total_fee: any,
	order_body: any,
	payStatus: any
) {
	let body = {
		payorder_no,
		total_fee,
		order_body,
		payStatus
	}
	// window.location.href = '#/user'
	// //if(payStatus)
	// alert("页面返回:payorder_no=" + payorder_no + "++total_fee=" + total_fee + "++order_body=" + order_body + "++payResult=" + payStatus);
	// return body
}

// function shareCX (style:any,data:any) {
// 	var obj =  new window.Chengxin(style,data)
// 	obj.setShareData(style,data)
// }




export default {
	getBalanceExport:getBalanceExport,
	getWalletStatusExport:getWalletStatusExport,
	getToWithdrawExport:getToWithdrawExport,
	getCheckxinToken: getCheckxinToken,
	getStartPayment: getStartPayment,
	addNewFriend: addNewFriend,
	setfullscreen: setfullscreen,
}
